package huoye.shops.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.codeideology.android.lang.entity.Paging;
import com.codeideology.android.widget.recycler.RecyclerViewAdapter;
import com.codeideology.android.widget.swiperefresh.AXIRefreshLayout;

import java.util.List;

import huoye.shops.R;
import huoye.shops.utils.PullToReshHelper;


/**
 * Created by user on 2016/11/17.
 */

public abstract class PullToRefreshFragment<E extends MyPojo> extends
        BaseFragment implements RecyclerViewAdapter.OnItemClickListener<E>, PullToReshHelper.OnRefreshListener {


    protected AXIRefreshLayout mRefreshLayout;
    protected RecyclerView mRecycler;

    protected View emptyView;
    protected TextView emptyValue;

    protected RecyclerViewAdapter mAdapter;
    private Toolbar toolbar;
    private TextView tvTitle;
    private ImageView ivReturn;
    private RelativeLayout llTop;
    protected boolean refresh = true;
    public abstract RecyclerViewAdapter createAdapter();

    protected PullToReshHelper<RecyclerViewAdapter, E> pullToReshHelper;

    protected long currentTime;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mRefreshLayout = (AXIRefreshLayout) view.findViewById(R.id.rl_recyclerview_refresh);
        mRecycler = (RecyclerView) view.findViewById(R.id.list);
        RecyclerView.ItemDecoration decoration = getItemDecoration();


        if (decoration != null) {
            mRecycler.addItemDecoration(getItemDecoration());
        }
        mAdapter = createAdapter();
        emptyView = view.findViewById(R.id.empty_list_ll);
        pullToReshHelper = new PullToReshHelper<>(getActivity(), mRefreshLayout, mRecycler, this, isAlterManager);
        pullToReshHelper.setEmptyView(emptyView);
        pullToReshHelper.setAdapter(mAdapter);
        pullToReshHelper.setLayoutManager(getLayoutManager());
        pullToReshHelper.init();

        if(!isEnabledMore()){
            mRefreshLayout.setEnableLoadmore(false);
            mRefreshLayout.setLoadmoreFinished(true);
            mRefreshLayout.setEnableLoadmoreWhenContentNotFull(false);
        }

        return view;
    }

    boolean isAlterManager = false;
    public void setLayoutManager(){
        isAlterManager = true;
    }


    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    protected RecyclerView.LayoutManager getLayoutManager() {
        return null;
    }

    @Override
    public int getContentViewId() {
        return R.layout.pull_to_refresh_fragmetn;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {



    }

    Paging p;
    /**
     * 查询数据
     */
    public void query() {
        currentTime = System.currentTimeMillis() / 1000;
        p = new Paging();
        query(p);
    }

    public void processQuery(Paging paging) {
        query(paging);
    }

    public void query(Paging paging) {


//        showProcessingIndicator();
    }


    protected void handlerExecption(String msg, String code, Throwable e) {
        pullToReshHelper.validateEmpty();
//        hideProcessingIndicator();
    }


    public void setDataSet(List<E> data, Paging paging) {
        pullToReshHelper.setDataset(data, paging);
        hideProcessingIndicator();
    }

    @Override
    public boolean isQueryMore() {
        if(mAdapter == null || mAdapter.getListCount() == 0 || mAdapter.getListCount() < Paging.DEFAULT_PAGE_SIZE) {
            return false;
        }
        return true;
    }

    public boolean isEnabledMore(){
        return true;
    }

}
